Open In Colab

Image Processing LAB 1 -22.08.2023¶

In [ ]:
!pip install opencv-python
Requirement already satisfied: opencv-python in /usr/local/lib/python3.10/dist-packages (4.8.0.76)
Requirement already satisfied: numpy>=1.21.2 in /usr/local/lib/python3.10/dist-packages (from opencv-python) (1.23.5)
In [ ]:
import cv2
img = cv2.imread("/content/tiger.jpg")
img.shape
Out[ ]:
(1365, 2048, 3)

The above code snippet uses the OpenCV library, imported with the alias "cv2". It reads an image file named "tiger.jpg" located at the specified path "/content/" and stores it in the 'img' variable. The 'img.shape' command returns the dimensions of the image, providing its height, width, and the number of color channels.

In [ ]:
from google.colab.patches import cv2_imshow
cv2_imshow(img)

The code uses the cv2_imshow function from the google.colab.patches module to display images in Google Colab. The cv2_imshow function is specifically designed for this purpose. The img variable contains the image data to be displayed. This code is suited for Google Colab and won't work the same way in other environments.

In [ ]:
img[:,:,0].shape
Out[ ]:
(1365, 2048)

This code accesses the first color channel (channel 0) of the image stored in the variable img. It then retrieves the shape of this color channel, which indicates the dimensions of the channel (height and width).

In [ ]:
cv2_imshow(img[:,:,0])

This line utilizes the cv2_imshow function to display the image using only the first color channel.In our case the first color channel is Blue, so the blue color and the colours that contains blue will be illuminated as white pixels according to their blueness. Colour Channel: BGR

In [ ]:
cv2_imshow(img[:,:,1])

In this case the second color channel is green, so the green color and the colours that contains green will be illuminated as white pixel according to their greenness. Colour Channel: BGR

In [ ]:
cv2_imshow(img[:,:,2])

In this case the third color channel is red, so the red color and the colours that contains red will be illuminated as white pixel according to their redness. Colour Channel: BGR

In [ ]:
img2=cv2.imread("/content/tiger.jpg",0)
img2.shape
Out[ ]:
(1365, 2048)

"tiger.jpg" is loaded using the imread function. The second argument 0 indicates that the image should be loaded in grayscale. The img2.shape command is retrieve the dimensions of the loaded grayscale image

In [ ]:
cv2_imshow(img2)

cv2_imshow(img2) used for displaying the image.

In [ ]:
cv2.imwrite("test.png",img2)
Out[ ]:
True

The line cv2.imwrite("test.png", img2) saves the image represented by the img2 variable as a PNG file named "test.png" using the OpenCV library. This function is commonly used to store images after processing or modifications in a format that can be easily shared or used in other applications.

In [ ]:
import matplotlib.pyplot as plt
plt.imshow(img2, cmap='inferno')
Out[ ]:
<matplotlib.image.AxesImage at 0x7e812ac2b0a0>

This code snippet employs the matplotlib.pyplot library, imported as plt, to display an image. The imshow function is used to visualize the image stored in the variable img2, using the 'inferno' color map to represent the data's intensity.It is a Perceptually Uniform Sequential colormap. There are total 5 color maps 'viridis', 'plasma', 'inferno', 'magma', 'cividis'.

In [ ]:
plt.imshow(img)
Out[ ]:
<matplotlib.image.AxesImage at 0x7e812ab23520>

For displaying the image

In [ ]:
import numpy as np
dumy_img=np.zeros(img.shape, dtype=np.uint8)
dumy_img.dtype
Out[ ]:
dtype('uint8')

The code imports the NumPy library as np and creates a new image "dumy_img" filled with black pixels using the np.zeros function, matching the dimensions of the existing img.The variable "dumy_img" has an unsigned 8-bit integer data type (dtype).

In [ ]:
img = cv2.imread("/content/tiger.jpg")
dumy_img[:,:,0]=img[:,:,2]
dumy_img[:,:,1]=img[:,:,1]
dumy_img[:,:,2]=img[:,:,0]
plt.imshow(dumy_img)
Out[ ]:
<matplotlib.image.AxesImage at 0x7e812a83d510>

The code loads an image named "tiger.jpg" using OpenCV. It creates a new image called dumy_img and swaps the color channels of the original image, turning it from blue-green-red to red-green-blue. Finally, the modified image is displayed using plt.imshow() for visualization.

In [ ]:
dumy_img=np.zeros(img.shape)
dumy_img[:,:,0]=img[:,:,2]
dumy_img[:,:,1]=img[:,:,1]
dumy_img[:,:,2]=img[:,:,0]
plt.imshow(dumy_img/255.)
Out[ ]:
<matplotlib.image.AxesImage at 0x7e8122a9bca0>

The code snippet creates a new image named dumy_img with the same dimensions as the input image img. The RGB channels are rearranged so that the red channel becomes blue, the green channel remains green, and the blue channel becomes red. Finally, the adjusted dumy_img is displayed using plt.imshow(), after scaling pixel values to the range [0, 1] for proper visualization.

In [ ]:
(dumy_img/255.).dtype
Out[ ]:
dtype('float64')

The expression "dumy_img/255.0" is used to scale down the pixel values of the image stored in "dumy_img." By dividing all pixel values by 255.0, which is the maximum pixel value in an 8-bit image, the image's colors are normalized to a range between 0 and 1 for proper display. This normalization is commonly done to ensure that the image is displayed correctly when using functions like plt.imshow.

In [ ]:
np.max(dumy_img[:200,:200,:])
Out[ ]:
221.0

The provided code calculates the maximum pixel value within the top-left region (200x200 pixels) of the "dumy_img" image array. This function returns the highest pixel value present in that specific portion of the image.

In [ ]:
cv2.imwrite('test2.png',dumy_img)
Out[ ]:
True

The line cv2.imwrite("test2.png", dumy_img) saves the image represented by the dumy_img variable as a PNG file named "test2.png"

In [ ]:
img_blk=np.zeros((512,512,3),np.uint8)
cv2_imshow(img_blk)

The code initializes a black image called "img_blk" with dimensions 512x512 pixels and three color channels. The image is of data type 'uint8,' which represents unsigned 8-bit integers. Then, it uses the cv2_imshow function to display this black image, showing a black square in the output.

In [ ]:
img_crop = cv2.imread("/content/tiger.jpg")
plt.imshow(img_crop[200:1200,600:1600,2])
Out[ ]:
<matplotlib.image.AxesImage at 0x7e8122298790>

The code loads an image named "tiger.jpg" and assigns it to "img_crop." Using plt.imshow(), it displays a cropped section of the image. The section displayed spans rows 200 to 1200 and columns 600 to 1600, emphasizing the blue channel (channel index 2) of the image. This showcases a visual representation of the specified region's blue color content.